package com.zzptc.ly.myapplication.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.core.widget.ListViewAutoScrollHelper;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.zzptc.ly.myapplication.Adapter.ContactAdapter;
import com.zzptc.ly.myapplication.Bean.User;
import com.zzptc.ly.myapplication.ChatActivity;
import com.zzptc.ly.myapplication.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
private ListView contactlv;
List<User> userList=new ArrayList<>();
ContactAdapter adapter;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initView(view);
        setListener();
        return view;
    }
//添加点击事件
    private void setListener() {
        contactlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent it=new Intent(getContext(), ChatActivity.class);
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putInt("friendid",userList.get(position).getId());
                editor.putString("friendname",userList.get(position).getName());
                editor.apply();
             startActivity(it);
            }
        });
    }

    private void initView(View view) {
    contactlv=view.findViewById(R.id.contactlv);
    int uid=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE).getInt("myid",-1);
    userList= LitePal.where("id !=?",uid+"").find(User.class);
    adapter=new ContactAdapter(getContext(),R.layout.comtact_item,userList);
    contactlv.setAdapter(adapter);

    }
}